<?php
Globals::requireClass('Controller');
Globals::requireModel('User');
Globals::requireTable('User');
Globals::requireTable('Indexlist');
Globals::requireTable('Bottomlist');

class IndexlistController extends Controller
{
	protected $UserTable;
	protected $IndexlistTable;
	protected $BottomlistTable;
	
	public static $defaultConfig = array(
		'viewEnabled'	=> true,
		'layoutEnabled'	=> true,
		'title'			=> null
	);
	
	public function __construct($config = null)
	{
		parent::__construct($config);
		$this->UserTable = new UserTable($config);
		$this->IndexlistTable = new IndexlistTable($config);
		$this->BottomlistTable = new BottomlistTable($config);
	}
	
	public function indexAction()
	{
	    $re = array(
	        'returncode' => 0,
	        'message' => '',
	        'toplisttime' => '',
	        'newsdatatime' => '',
	        'generalizelisttime' => '',
	        'vlivelisttime' => '',
	        'toplist' => array(),
	        'newsdata' => array('icon'=>'','newslist'=>array()),
	        'generalizelist' => array(),
	        'vlivelist' => array()
	    );
	    
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        $re['returncode'] = -1;
	        $re['message'] = '用户信息不正确';
	        echo json_encode($re);
	        exit();
	    }
	    
	    $machine_type_value = $machine_type + 1;
	    
	    $toplisttime = $this->getParam('toplisttime');
	    $newsdatatime = $this->getParam('newsdatatime');
	    $generalizelisttime = $this->getParam('generalizelisttime');
	    $vlivelisttime = $this->getParam('vlivelisttime');
	    
	    $type_time_1 = $this->IndexlistTable->getRowWithFields('createtime',array('type'=>1,'status'=>1),'createtime desc');
	    if($type_time_1['createtime'] > $toplisttime){
	        $re['toplisttime'] = $type_time_1['createtime'];
	        
	        $type_data_1 = $this->IndexlistTable->listAll(array('type'=>1,'status'=>1),'rank asc');
	        foreach ($type_data_1 as $v){
	            if($v['machine_type'] == 0 || $v['machine_type'] == $machine_type_value){
	                $re['toplist'][] = array(
	                    'title' => $v['title'],
	                    'icon' => $v['icon'],
	                    'url' => $v['link']
	                );
	            }
	        }
	    }else{
	        $re['toplisttime'] = $toplisttime;
	    }
	    
	    
	    $type_time_2 = $this->IndexlistTable->getRowWithFields('createtime',"status = 1 and (type = 2 or type = 3)",'createtime desc');
	    if($type_time_2['createtime'] > $newsdatatime){
	        $re['newsdatatime'] = $type_time_2['createtime'];
	        
	        $type_data_2 = $this->IndexlistTable->listAll(array('type'=>2,'status'=>1),'rank asc');
	        foreach ($type_data_2 as $v){
	            if($v['machine_type'] == 0 || $v['machine_type'] == $machine_type_value){
    	            $re['newsdata']['newslist'][] = array(
    	                'title' => $v['title'],
    	                'icon' => $v['icon'],
    	                'url' => $v['link']
    	            );
	            }
	        }
	        
	        $type_data_3 = $this->IndexlistTable->getRow("type = 3 and status = 1 and machine_type in (0,{$machine_type_value})",'rank asc');
	        $re['newsdata']['icon'] = $type_data_3['icon'];
	        
	    }else{
	        $re['newsdatatime'] = $newsdatatime;
	    }
	    
	    
	    $type_time_4 = $this->IndexlistTable->getRowWithFields('createtime',array('type'=>4,'status'=>1),'createtime desc');
	    if($type_time_4['createtime'] > $generalizelisttime){
	        $re['generalizelisttime'] = $type_time_4['createtime'];
	        
	        $type_data_4 = $this->IndexlistTable->listAll(array('type'=>4,'status'=>1),'rank asc');
	        foreach ($type_data_4 as $v){
	            if($v['machine_type'] == 0 || $v['machine_type'] == $machine_type_value){
    	            $re['generalizelist'][] = array(
    	                'icon' => $v['icon'],
    	                'url' => $v['link']
    	            );
	            }
	        }
	    }else{
	        $re['generalizelisttime'] = $generalizelisttime;
	    }
	    
	    
	    $type_time_5 = $this->IndexlistTable->getRowWithFields('createtime',array('type'=>5,'status'=>1),'createtime desc');
	    if($type_time_5['createtime'] > $vlivelisttime){
	        $re['vlivelisttime'] = $type_time_5['createtime'];
	        
	        $type_data_5 = $this->IndexlistTable->listAll(array('type'=>5,'status'=>1),'rank asc');
	        foreach ($type_data_5 as $v){
	            if($v['machine_type'] == 0 || $v['machine_type'] == $machine_type_value){
    	            $re['vlivelist'][] = array(
    	                'title' => $v['title'],
    	                'icon' => $v['icon'],
    	                'describe' => $v['remark'],
    	                'url' => $v['link']
    	            );
	            }
	        }
	    }else{
	        $re['vlivelisttime'] = $vlivelisttime;
	    }
	    
	    $re['message'] = '成功';
	    echo json_encode($re);
	    exit();
	}
	
	
	public function bottomAction()
	{
	    $re = array(
	        'returncode' => 0,
	        'message' => '',
	        'vbottomtime' => '',
	        'bottomlist' => array()
	    );
	    
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        $re['returncode'] = -1;
	        $re['message'] = '用户信息不正确';
	        echo json_encode($re);
	        exit();
	    }
	    
	    $machine_type_value = $machine_type + 1;
	    $vbottomtime = $this->getParam('vbottomtime');
	    
	    $type_time = $this->BottomlistTable->getRowWithFields('createtime',array('status'=>1),'createtime desc');
	    if($type_time['createtime'] > $vbottomtime){
	        $re['vbottomtime'] = $type_time['createtime'];
	        
	        $type_data = $this->BottomlistTable->listAll(array('fid'=>0,'status'=>1),'rank asc');
	        foreach ($type_data as $v){
	            $tmp = array('title'=>$v['title'],'list'=>array());
	            
	            $tmp_data = $this->BottomlistTable->listAll(array('fid'=>$v['id'],'status'=>1),'rank asc');
	            foreach ($tmp_data as $v2){
	                if($v2['machine_type'] == 0 || $v2['machine_type'] == $machine_type_value){
    	                $tmp['list'][] = array(
    	                    'title' => $v2['title'],
    	                    'url' => $v2['link']
    	                );
	                }
	            }
	            
	            $re['bottomlist'][] = $tmp;
	        }
	    }else{
	        $re['vbottomtime'] = $vbottomtime;
	    }
	    
	    $re['message'] = '成功';
	    echo json_encode($re);
	    exit();
	}
	
	
	protected function out()
	{
		$this->layout->nav		= 'index';
		parent::out();
	}
}

Config::extend('IndexlistController', 'Controller');
